package cn.webestar.scms.controller;

import cn.webestar.scms.api.vo.department.DepartmentVO;
import cn.webestar.scms.commons.R;
import cn.webestar.scms.commons.api.PageVO;
import cn.webestar.scms.security.Authentication;
import cn.webestar.scms.security.SessionContextHolder;
import cn.webestar.scms.user.api.DepartmentApi;
import cn.webestar.scms.user.api.dto.Department.DepartmentLimitDTO;
import cn.webestar.scms.user.api.dto.Department.DepartmentRootPageDTO;
import cn.webestar.scms.user.api.dto.Department.DepartmentUpdateDTO;
import cn.webestar.scms.user.entity.Department;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Set;

@Slf4j
@Validated
@RestController
@Tag(name = "部门接口")
@RequestMapping("/api/department")
public class DepartmentController {

    @Autowired
    private DepartmentApi departmentApi;

    @PostMapping("/deleteByIds")
    public R<Boolean> deleteByIds(@RequestBody Set<Long> ids){
        return departmentApi.deleteByIds(ids);
    }

//    @PostMapping("/save")
//    public R<Department> save(@RequestBody DepartmentUpdateDTO body){
//        Authentication auth = SessionContextHolder.getContext().getAuth();
//        body.setCid(auth.getCid());
//        return departmentApi.save(body);
//    }
//
//    @PostMapping("/findDepTree")
//    public R<PageVO<DepartmentVO>> findDepTree(@RequestBody DepartmentRootPageDTO pageDTO){
//
//        Authentication auth = SessionContextHolder.getContext().getAuth();
//        pageDTO.setCid(auth.getCid());
//
//        // 查询根部门
//        R<PageVO<Department>> rootDepPageR = departmentApi.pageRoot(pageDTO);
//        PageVO<Department> rootDepPage = rootDepPageR.getData();
//        List<DepartmentVO> rootDeps = DepartmentVO.convertToList(rootDepPage.getRecords());
//
//        // 查询所有部门
//        DepartmentLimitDTO limitDTO = new DepartmentLimitDTO();
//        limitDTO.setCid(auth.getCid());
//        limitDTO.setStatus(1);
//        R<List<Department>> allDepR = departmentApi.limitList(limitDTO);
//        List<DepartmentVO> allDeps = DepartmentVO.convertToList(allDepR.getData());
//
//        // 设置子部门
//        List<DepartmentVO> rootDepsWithChildren = buidDeptTree(rootDeps, allDeps);
//        return R.success(new PageVO<>(rootDepPage.getPageNum(), rootDepPage.getPageSize(), rootDepPage.getTotalCount(), rootDepsWithChildren));
//    }

    private List<DepartmentVO> buidDeptTree(List<DepartmentVO> rootDeps, List<DepartmentVO> allDeps){
        for (DepartmentVO rootDep : rootDeps) {
            setChildren(rootDep, allDeps);
        }
        return rootDeps;
    }

    private void setChildren(DepartmentVO parent, List<DepartmentVO> deps){
        List<DepartmentVO> childrens = new ArrayList<>();
        for (DepartmentVO dep : deps){
            if (Objects.equals(dep.getPid(), parent.getId())){
                setChildren(dep, deps);
                childrens.add(dep);
            }
        }
        parent.setChildren(childrens);
    }

}
